use mmu::{
    address::*,
    memory::*,
};

#[test]
fn test_read_write_bytes() {
    let paddr: PhysAddr = 0xFF.into();
    memory_write_bytes(paddr, b"hello, world!");
    let mut bytes = [0u8; 32];
    memory_read_bytes(paddr, &mut bytes[..13]);
    assert_eq!(&bytes[..13], b"hello, world!");
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
struct Data {
    bl: bool,
    b: i8,
    s: i16,
    i: i32,
    l: i64,
    c: char,
}

impl Data {
    fn new(bl: bool, b: i8, s: i16, i: i32, l: i64, c: char) -> Self {
        Self {
            bl, b, s, i, l, c
        }
    }
}

#[test]
fn test_get_ref_mut_base_type() {
    let paddr: PhysAddr = 40960.into();
    let value = memory_get_mut::<usize>(paddr);
    *value = 0xFFFFFFFFFFFFFFFF;
    let value = *memory_get_ref::<usize>(paddr);
    assert_eq!(value, 0xFFFFFFFFFFFFFFFF);

    let paddr: PhysAddr = 4096.into();
    let value = memory_get_mut::<bool>(paddr);
    *value = false;
    let value = *memory_get_ref::<bool>(paddr);
    assert_eq!(value, false);    
}

#[test]
fn test_get_ref_mut_struct_type() {
    let data = Data::new(false, 7, 88, 3122, 221234, '哈');
    let paddr: PhysAddr = (4096 + 128).into();
    *memory_get_mut::<Data>(paddr) = data;
    let copy_data = *memory_get_ref::<Data>(paddr);
    assert_eq!(copy_data.c, '哈');
    assert_eq!(data, copy_data);
}
